/**
 * Created by Administrator on 2018-09-09.
 * 消息推送
 */

define(function (require,exports,module) {
    require("./messagePush.css");
    require("../../lib/fullcalendar/moment.min.js");
    var socket=require("../util/socketServer.js");
    var sTpl = require("./messagePush.html");
    var VueComponent = Vue.extend({
        props:['parentData'],
        components:{
            'socket':socket
        },
        template: sTpl,
        methods:{
            seeMessage:function () {
                this.slidingWindowVisible1 = true;
            },
            close:function () {
                this.slidingWindowVisible1=false;
            },
            /*查询未处理的数据有几条*/
            findNotHandelMg:function () {
                var $this = this;
                axios.get(basepath+"/api/mgp/findNotHandelMg",{
                    params: {
                         mgType:$this.parentData.messageType,
                        handle:false,
                    }}
                ).then(function (response) {
                    if(response.data!=null){
                        $this.notHandelMg=response.data;
                        messages.$emit('notHandelMgCount',$this.notHandelMg);
                    }

                });
            },
            /*当前页*/
            findMg:function () {
                this.handelMg=[];
                this.buffernHandelMg=[];
                this.hasBuffernHandelMg=false;
                this.hasNext=false;
                var $this = this;
                axios.get(basepath+"/api/mgp/findAllMp",{
                    params: {
                        mgType:$this.parentData.messageType,
                        handle:!$this.checkMg.handel?false:true,
                        mgContent:$this.checkMg.mgContent,
                        page:0,
                        size:21,
                        sort:'pushTime,desc'
                    }}
                ).then(function (response) {
                    /*如果小于20条没有下一页*/
                    if(response.data.content.length<=20){
                        $this.hasNext=false;
                        /*如果小于11条则没有缓存的内容*/
                        if(response.data.content.length<11){
                            $this.handelMg=response.data.content;
                            $this.hasBuffernHandelMg=false;
                        }else if(response.data.content.length>=11){
                            $this.handelMg=response.data.content.slice(0,10);
                            $this.buffernHandelMg=response.data.content.slice(10);
                            $this.hasBuffernHandelMg=true;
                        }
                    }else if(response.data.content.length>20){
                        $this.handelMg=response.data.content.slice(0,10);
                        $this.buffernHandelMg=response.data.content.slice(10,20);
                        $this.hasBuffernHandelMg=true;
                        $this.hasNext=true;
                    }
                });

            },
            /*更多*/
            findMoreMg:function () {
                this.handelMg=this.handelMg.concat(this.buffernHandelMg);
                this.$set(this,"handelMg",this.handelMg);
                var $this = this;
               axios.get(basepath+"/api/mgp/findAllMp",{
                    params: {
                       mgType:$this.parentData.messageType,
                        handle:!$this.checkMg.handel?false:true,
                        mgContent:$this.checkMg.mgContent,
                        page:$this.pageSN,
                        size:10,
                        sort:'pushTime,desc'
                    }}
                ).then(function (response) {
                    if(response.data.content!=null){
                        $this.buffernHandelMg=response.data.content;
                        $this.hasBuffernHandelMg=true;
                        $this.pageSN++;
                    }else if(response.data.content==null){
                        $this.hasBuffernHandelMg=false;
                    }
                    $this.loading=false;

                });

            },
            handled:function (index) {
                $this=this;
                var mg=this.handelMg[index]
                if(!mg.handle) {
                    mg.handle = true;
                    axios.post(basepath + '/api/mgp/saveMp',mg)
                        .then(function (response) {
                            $this.$notify({
                                title: '处理成功',
                                message: '已处理'
                            });
                            $this.notHandelMg--;
                            messages.$emit('notHandelMgCount',$this.notHandelMg);


                        });

                }
            },
            seed:function (index) {
                $this=this;
                var mg=this.handelMg[index]
                if(!mg.see){
                    mg.see=true;
                    axios.post(basepath+'/api/mgp/saveMp',mg)
                        .then(function (response) {
                            $this.$notify({
                                title: '已查阅',
                                message: '已查阅'
                            });
                        });
                }
            },
        },
        data:function(){
            return {
                notHandelMg:0,
                hasBuffernHandelMg:true,
                loading:false,
                hasNext:false,
                checked:true,
                slidingWindowVisible1: false,
                handelMg:[],
                buffernHandelMg:[],
                checkMg:{
                    mgContent:'',
                    handel:true,
                },
                pageSN:2,
            }
        },
        filters: {
            pushTimeFormat: function (value) {
                if (!value) return '';
                var date = new Date(value.replace(/-/g,"/") );
                return date.getMonth()+1+'月'+date.getDate()+'日'+' '+date.getHours()+':'+date.getMinutes()+' ';
            }
        },

        mounted:function() {

            var container =$(".mp .msg-container")
            var containerHeight= container.height();
            $this=this;
            container.scroll(function() {
                var contextHeight = $(".mp .msg-container .scrollContext").height();
                var container =$(".mp .msg-container");
                var now=container.scrollTop()+containerHeight-40;
                if(contextHeight-now<=40&& $this.hasBuffernHandelMg&&!$this.loading ){
                    $this.loading=true;
                    $this.findMoreMg();
                }


            });

        },

        deactivated:function () {
            messages.$off(this.parentData.messageType);
            messages.$emit("hasMessage",false);
        },
        activated:function () {
            this.findMg();
            this.findNotHandelMg();
            messages.$emit('change',"消息推送测试");
            messages.$emit("hasMessage",true);
            $this=this;
            messages.$on(this.parentData.messageType,function (value) {
                $this.handelMg.unshift(JSON.parse(value.data).messageBody);
                $this.$set($this.handelMg,"handelMg",$this.handelMg);
                $this.notHandelMg++;
                messages.$emit('notHandelMgCount',$this.notHandelMg);
                $this.$message({
                    showClose: true,
                    type:'info',
                    message: JSON.parse(value.data).mgContent,
                    duration:30000
                });

            });

        },
        created:function (){

        }
    });

    module.exports = VueComponent;
})
